//
//  AvaliableCouponViewController.m
//  FD
//
//  Created by Yasin on 2018/10/26.
//  Copyright © 2018 ZhiYuan Network. All rights reserved.
//  可用优惠券页面

#import "AvaliableCouponViewController.h"
//Cell
#import "AvaliableCouponCell.h" //xib 弃用❌
#import "MyKaQuanCell.h" //纯代码 ✅

#import "FDNowDataView.h"

//model
#import "AvaliableCouponModel.h"

@interface AvaliableCouponViewController ()
<UITableViewDelegate,
UITableViewDataSource>

/**tableView*/
@property (nonatomic,  strong) UITableView *tv;
/**优惠券数组*/
@property (nonatomic,  strong) NSMutableArray *couponArr;
/**1-3无数据占位图*/
@property (nonatomic, strong) FDNowDataView *nodataView;

@end

@implementation AvaliableCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}
- (void)setupView {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSLocalizedString(Order_AvaliableCoupon, @"可用优惠券");
    
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    self.tv.tableFooterView = [UIView new];
    
    //无数数据占位图
    [self.tv addSubview:self.nodataView];
    self.nodataView.hidden = YES;//默认隐藏
}

- (void)setCouponListArr:(NSArray *)couponListArr {
    _couponListArr = couponListArr;
    
    //解析数据
    for (NSDictionary *dic in couponListArr) {
        NSDictionary *couponDic = dic[@"coupon"];
        AvaliableCouponModel *model  = [AvaliableCouponModel mj_objectWithKeyValues:couponDic];
        [self.couponArr addObject:model];
    }
    
    //刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        if (couponListArr.count) {
            self.nodataView.hidden = YES;
        }else {
            self.nodataView.hidden = NO;
        }
        [self.tv reloadData];
    });
}


#pragma mark - getter
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册cell
//        [self.tv registerNib:[UINib nibWithNibName:@"AvaliableCouponCell" bundle:nil] forCellReuseIdentifier:@"AvaliableCouponCell"];
        [self.tv registerClass:[MyKaQuanCell class] forCellReuseIdentifier:@"MyKaQuanCell"];

        //取消线
        self.tv.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tv;
}

- (FDNowDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[FDNowDataView alloc] initWithFrame:
                    CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - FD_NavH)];
    }
    return _nodataView;
}

- (NSMutableArray *)couponArr {
    if (!_couponArr) {
        _couponArr = [NSMutableArray array];
    }
    return _couponArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.couponArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    AvaliableCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AvaliableCouponCell"];
//    cell.model = self.couponArr[indexPath.row];
    
    MyKaQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyKaQuanCell"];
    cell.avaliableModel = self.couponArr[indexPath.row];
    
    
    
    //2019-2-26点击事件
    FDWeakSelf;
    cell.clickAvaliableCouponUseNowBlock = ^(AvaliableCouponModel *model) {
//        FDLog(@"%@",model);
        //将优惠quan 回传过去
        if ([weakSelf.delegate respondsToSelector:@selector(didSelectedAvaliableCoupon:)]) {
            [weakSelf.delegate didSelectedAvaliableCoupon:model];
            
            //原路返回
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    };
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}

//!!!!:2019-2-26不要了
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    //取出对应的优惠券model
//    AvaliableCouponModel *couponModel = self.couponArr[indexPath.row];
//
//    FDLog(@"优惠券:%@--%ld",couponModel.rule,couponModel.ruleGoodsArr.count);
//    //将优惠quan 回传过去
//    if ([self.delegate respondsToSelector:@selector(didSelectedAvaliableCoupon:)]) {
//        [self.delegate didSelectedAvaliableCoupon:couponModel];
//
//        //原路返回
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}

#pragma mark - 请求数据 1-10 不调接口了从上一个页面传递过来。
//- (void)loadAvaliableCouponDataWithOrderAmount:(NSNumber *)orderAmount {
//
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//
//    [FDHttpAPi requestDDOrderCouponWithOrderAmount:orderAmount successBlock:^(id responseObjc) {
//        [hud hideAnimated:YES];
//
//
//        NSString *event = responseObjc[@"event"];
//        if ([event isEqualToString:SUCCESS]) { //请求成功解析数据
//            NSArray *arr = responseObjc[@"data"];
//
//            for (NSDictionary *dic in arr) {
//                NSDictionary *couponDic = dic[@"coupon"];
//                AvaliableCouponModel *model  = [AvaliableCouponModel mj_objectWithKeyValues:couponDic];
//                [self.couponArr addObject:model];
//            }
//
//            //刷新
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.tv reloadData];
//
//                if (arr.count) {
//                    self.nodataView.hidden = YES;
//                }else {
//                    self.nodataView.hidden = NO;
//                }
//            });
//
//        }else { //弹框提示
//            [JRToast showWithText:responseObjc[describe]];
//            self.nodataView.hidden = NO;
//        }
//    } failBlock:^(NSError *error) {
//        FDLog(@"%@",[error localizedDescription]);
//        [hud hideAnimated:YES];
//        self.nodataView.hidden = NO;
//    }];
//}

@end
